$(document).ready(function(e){
	createPageTab();
	createPositionListTab();
	//setPosition("positionSelect");
	findList(1,showData);
});

var totalPageNum = 0;
var isDesc = "desc";
var list = null;
var curPageNum = null;
function findList(pageNow,callback){
	var tmp2=root+"/admin/position!findList";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc},
		dataType:"json",
		success: function(json){
			callback(json);
		}
	});
}

function showData(json){
	if(list!=null) createPositionListTab();
	totalPageNum = json.totalPageNum;
	list = json.pageData;
	curPageNum = json.curPageNum;
	//写入系统list
	var htmlStr = '';
	for(var i=0; i<json.pageData.length; i++){
		htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
		htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
		htmlStr += '</div></td>';
		htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
		htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
		htmlStr += '<td height="24" bgcolor="#FFFFFF">';
		htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].name+'</div></td>';
		htmlStr += '<td height="24" bgcolor="#FFFFFF">';
		htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
		htmlStr += '<td height="24" bgcolor="#FFFFFF">';
		htmlStr += '<div align="center" class="STYLE2 STYLE1"><a href="'+root+'/admin/employee!goEmployeeJsp?position.id='+json.pageData[i].id+'">查看职员</a></div></td>';
		htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
		htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
		htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdate('+json.curPageNum+','+i+')">编辑</a>';
		htmlStr += '<span class="STYLE1">]</span></div></td>';
		htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
		htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
		htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="del('+json.pageData[i].id+')">删除</a>';
		htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
	}
	$("#listInfoTab").append(htmlStr);
	
	//写入分页信息
	$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
	$("#onePage").unbind("click");
	$("#onePage").bind("click",function(){findList(1,showData);});
	if(json.curPageNum>1){
		$("#perPage").unbind("click");
		$("#perPage").bind("click",function(){findList((json.curPageNum-1),showData);});
	}
	if((json.curPageNum+1)<=json.totalPageNum){
		$("#nextPage").unbind("click");
		$("#nextPage").bind("click",function(){findList((json.curPageNum+1),showData);});
	}
	$("#lastPage").unbind("click");
	$("#lastPage").bind("click",function(){findList(json.totalPageNum,showData);});

}


function showAdd(){
	$("#backGroud").show();
	$("#addDiv").show();
}

function closeAdd(){
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function add(){
	if($("#positionName_add").val()=="" || $("#positionName_add").val()==null){
		alert("职位不能为空！");
		return;
	}
	var tmp2=root+"/admin/position!verificationName";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"position.name":$("#positionName_add").val()},
		dataType:"json",
		success: function(json){
			if(json.code=="y"){
				alert("已经拥有改职位，请选择另外的职位名");
				return;
			}else{
				$("#addForm").submit();
			}
		}
	});
}

function del(id){
	var ids = "";
	if(id==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一个职位！");
			return;
		}
	}
	
	var tmp2=root+"/admin/position!delete";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"position.id":id, "ids":ids, "page.curPageNum":curPageNum, "isDesc": isDesc},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			findList(json.curPageNum,showData);
		}
	});
}

function showUpdate(pageNow, index){
	$("#positionId_u").val(list[index].id);
	$("#positionName_u").val(list[index].name);
	
	$("#backGroud").show();
	$("#updateDiv").show();
	
	$("#updateButton").bind("click",function(){update(pageNow, index);});
}

function closeUpdate(){
	$("#updateForm")[0].reset();
	$("#updateButton").unbind("click");
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function update(pageNow, index){
	if($("#positionName_u").val()=="" || $("#positionName_u").val()==null){
		alert("用户名不能为空！");
		return;
	}
	var tmp2=root+"/admin/position!update";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"position.id":$("#positionId_u").val(), "position.name": $("#positionName_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdate();
			if(json.code=="n"){
				alert("修改失败！");
			}else{
				findList(json.curPageNum,showData);
			}
		}
	});
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findList(p);
	}
}